from base_page import BaseHandler

import base
import data

class CreateProfileHandler(BaseHandler):
  def PreCheckOrRedirect(self):
    valid_types = ['donor', 'manufacturer']
    self.profile_type = self.request.get('type')
    if self.profile_type not in valid_types:
      self.RedirectAndRaise("/")

  def ProcessGet(self):
    self.Setup('create_profile.html', required_registered=True)
    self.template_values["type"] = self.profile_type
    self.Render()

  def ProcessPost(self):
    self.Setup('create_profile.html', required_registered=True)
    if self.profile_type == 'donor':
      profile = data.DonorProfile() 
      create_now = self.user.is_sys_admin or self.user.is_donor_mod
      active_state = data.DonorProfile.ACTIVE
      pending_state = data.DonorProfile.PENDING
    elif self.profile_type == 'manufacturer':
      profile = data.ManufacturerProfile()
      create_now = self.user.is_sys_admin or self.user.is_manufacturer_mod
      active_state = data.ManufacturerProfile.ACTIVE
      pending_state = data.ManufacturerProfile.PENDING
    else:
      self.RedirectAndRaise("/")  # should not get here
    # Note: Donor and Manufacturer profiles have same fields.
    # So we can set both fields here.
    profile.creator = self.user
    profile.name = self.GetFieldOrRedirect('name')
    profile.description = self.GetFieldOrRedirect('description')
    profile.email = self.request.get('email')
    profile.phone = self.request.get('phone')
    profile.address = self.request.get('address')
    profile.website = base.MaybePrependHttp(self.request.get('website'))
    if create_now:
      profile.state = active_state
    else:
      profile.state = pending_state
    profile.put()
    self.redirect("/profile")


